package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"vicente-gin/common"
	"vicente-gin/dto"
	"vicente-gin/middleware"
	"vicente-gin/service"
	"vicente-gin/utils"
)

// @Summary 用户列表
// @Description 获取用户列表
// @Tags 用户管理
// @accept json
// @Produce  json
// @Param Authorization header   string true "token"
// @Param id query int false "id"
// @Param username query string false "用户名"
// @Param phone query string false "电话号"
// @Param deptId query string false "部门ID"
// @Param pageSize query string false "每页条数"
// @Param page query string false "页数"
// @Success 200 {object} middleware.Response{}
// @response     default          {string}  string    "other error"
// @Header       200              {string}  Location  "/entity/1"
// @Header       200,400,default  {string}  Token     "token"
// @Header       all              {string}  Token2    "token2"
// @Router /admin/user/list [get]c
func GetUserList(c *gin.Context) {
	fmt.Println("8888888888888888888888888888888")
	claims, _ := c.Get("claims")
	fmt.Printf("%+v", claims)
	fmt.Println("9999999999999999999999999999999")
	var q dto.QueryUser
	if err := c.ShouldBind(&q); err != nil {
		middleware.ResponseFail(c, 201, err.Error())
		return
	}
	common.LOG.Debug("this is debug message")
	common.LOG.Info("this is info message")
	common.LOG.Info("this is info message with fileds",
		zap.Int("age", 24), zap.String("agender", "man"))
	common.LOG.Error("this is error message")
	common.LOG.Error("this is Error message with fileds",
		zap.Int("age", 24), zap.String("agender", "man"))
	users, total, err := service.GetUserList(q)
	if err != nil {
		middleware.ResponseFail(c, 202, err.Error())
	} else {
		middleware.ResponseSucc(c, "获取用户列表成功", map[string]interface{}{
			"items": users,
			"total": total,
		})
	}
	return
}

// @Summary 创建用户
// @Description 创建用户
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param Authorization header   string true "token"
// @Param param body dto.UserInfoIn false "参数"
// @Success 200 {object} middleware.Response{}
// @Router /admin/user/add [post]
func CreateUser(c *gin.Context) {
	var user dto.UserInfoIn
	err := c.BindJSON(&user)
	if err != nil {
		middleware.ResponseFail(c, 201, err.Error())
		return
	}
	err = service.SaveUser(user)
	if err != nil {
		middleware.ResponseFail(c, 202, err.Error())
	} else {
		middleware.ResponseSucc(c, "添加用户成功", true)
	}
	return
}

/*func GetUserMenuByClaims(c *gin.Context) {
	userInfo:=middleware.GetClaims(c)
	if  ""!=userInfo.UserID{
		menus, err := service.GetRoleMenuTree(utils.StrToInt(userInfo.RoleId))

		if err != nil {
			middleware.ResponseFail(c, 201, err.Error())
		} else {
			middleware.ResponseSucc(c, "获取菜单列表成功", menus)
		}

		return
	}
}*/

// @Summary 修改密码
// @Description 修改密码
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param Authorization header   string true "token"
// @Param param body dto.PassWord false "参数"
// @Success 200 {object} middleware.Response{}
// @Router /admin/password [post]
func ResetPwd(c *gin.Context) {
	var pwdForm dto.PassWord
	if err := c.ShouldBindJSON(&pwdForm); err != nil {
		middleware.ResponseFail(c, 202, err.Error())
		return
	}
	err1 := service.ResetPassword(pwdForm)
	if err1 != nil {
		middleware.ResponseFail(c, 202, err1.Error())
	} else {
		middleware.ResponseSucc(c, "重置密码成功", true)
	}
	return
}

func UpdateUser(c *gin.Context) {
	var user dto.UserInfoIn
	err := c.BindJSON(&user)
	if err != nil {
		middleware.ResponseFail(c, 201, err.Error())
		return
	}
	err = service.SaveUser(user)
	if err != nil {
		middleware.ResponseFail(c, 202, err.Error())
	} else {
		middleware.ResponseSucc(c, "更新用户成功", true)
	}
	return
}

func DelUser(c *gin.Context) {
	var user dto.QueryUser
	err := c.ShouldBindJSON(&user)
	if err != nil {
		middleware.ResponseFail(c, 201, err.Error())
		return
	}
	err = service.DeleteUser(utils.StrToInt(user.ID))
	if err != nil {
		middleware.ResponseFail(c, 202, err.Error())
	} else {

		middleware.ResponseSucc(c, "删除用户成功", true)

	}
	return
}
